var PrePlan=require('../models/guestPrePlanModel.js');
var Exercise=require('../models/exerciseModel.js');

var code=require('../txtCode/code.js');
module.exports={
	GetPageQuantity:function(req,res,next){
		if(req.session){
		PrePlan
		.find()
		.sort()
		.count(function(err,count){
			if(err) console.log(err);
			console.log(count);
			var pageQuantity=Math.ceil(count/10);
			res.send({
				code:code.success,
				data:{'pageQuantity':pageQuantity}});
			});
		}//if(req.session)
	},//GetPageQuantity
	PageQuantity:function(req,res,next){
		if(req.session){
			var mobile=req.body.mobile;
			console.log(mobile);
			PrePlan
			.find({mobile:mobile})
			.sort()
			.count(function(err,count){
				if(err) console.log(err);
				console.log(count);
				var pageQuantity=Math.ceil(count/10);
				res.send({
					code:code.success,
					data:{'pageQuantity':pageQuantity}
				});
			})//PrePlan
		}//if(req.session)
	},//PageQuantity
	FirstShow:function(req,res,next){
		if(req.session){
			PrePlan
			.find()
			.sort()
			.limit(10)
			.then(
				function(result){
					res.send({code:code.success,data:result});
				}
				,function(err){
					if(err) console.log(err);
					res.send({code:code.findFail});
				}
				);//
			
		}//if(req.session)

	},//FirstPage
	ShowPage : function(req,res,next){
		if(req.session){
			var PAGE_SIZE=10;
			var pageNumber=(parseInt(req.body.pageNumber)-1)*PAGE_SIZE;
			PrePlan
			.find()
			.sort()
			.skip(pageNumber)
			.limit(10)
			.then(function(result){
				console.log(result);
				res.send({
					code:[1000,'success'],
					data:result
				});
			});
		}//if(req.session)
	},//ShowPage
	PageShowPage : function(req,res,next){
		if(req.session){
			var PAGE_SIZE=10;
			var condition=req.body.condition;
			var pageNumber=(parseInt(req.body.pageNumber)-1)*PAGE_SIZE;
			PrePlan
			.find(condition)
			.sort()
			.skip(pageNumber)
			.limit(10)
			.then(function(result){
				console.log(result);
				res.send({
					code:[1000,'success'],
					data:result
				});
			});
		}//if(req.session)
	},//ShowPage
	FetchPrePlan:function(req,res,next){
		if(req.session){
			var needPrePlanname=req.body.needPrePlanname;
			var needGuestname=req.body.needGuestname;
			PrePlan
			.find({ 
					guestname:needGuestname,
					prePlanname:needPrePlanname
				  })
			.then(function(result){
				res.send({
					code:[1000,'success'],
					data:result
				})

			})
		}//if(req.session)
	},//FetchMotion
	FetchGuestPreplan:function(req,res,next){
		if(req.session){
			var data=req.body;
			console.log(data);
			PrePlan
			.find(data)
			.exec(function(err,result){
				if(err) {
					console.log(err);
					res.send({
						code:[1001,'service is wrong'],
						data:err
					});
					return;
				};
				res.send({
					code:[1000,'success'],
					data:result
				});

			})
		}//if(req.session)
	},//FetchGuestPrePlan
	FetchPrePlanFormGuest:function(req,res,next){
		if(req.session){
			var needGuestname=req.body.needGuestname;
			PrePlan
			.find({
					guestname:needGuestname
			})
			.then(function(result){
				res.send({
					code:[1000,'success'],
					data:result
				})
			})
		}//if(req.session)
	},//FetchPrePlanFormGuest
SavePrePlan:function(req,res,next){
		if(req.session){
			var data=req.body;
			console.log(data);
			var oAddPrePlan=new PrePlan(data);
			oAddPrePlan.save(function(err){
				if(err) console.log(err);

				// var exercise1=new Exercise({
				// 	guestname:oAddPrePlan.guestname,
				// 	mobile:oAddPrePlan.mobile,
				// 	prePlanname:oAddPrePlan.prePlanname,
				// 	prePlanContent:oAddPrePlan._id
				// });//test

				// exercise1.save();
			}).then(
					function(data){
		 				res.send({code:code.success});	
		 			},
		 		function(err){
		 			console.log(err);
		 			res.send({code:code.insertFail});
		 		});//oAddMotion
		}//if(req.session)
	},//SaveAdd
VagueName:function(req,res,next){
		if(req.session){
			var regexString=req.body.vagueName;
			console.log(regexString);
			var regex=new RegExp(regexString);
			
			console.log(regex);
			PrePlan
			.find({'guestname':regex})
			.exec(function(err,items){
				if(err) console.log(err);
				console.log(items);
				res.send({
					data:items
				});
			});//Guest
		}//if(req.session)
	},//VagueName
}